package com.woniuxy.yoga.commons.alipay.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "alipay.config")
@Data
public class ALiPayConfiguration {
    private String appid;
    private String serverUrl;
    private String privateKey;
    private String alipayPublicKey;

    @Bean
    public AlipayClient config() throws Exception {
        AlipayConfig alipayConfig = new AlipayConfig();
        //设置网关地址
        alipayConfig.setServerUrl(serverUrl);
        //设置应用ID
        alipayConfig.setAppId(appid);
        //设置应用私钥
        alipayConfig.setPrivateKey(privateKey);
        //设置支付宝公钥
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        //设置请求格式固定值json
        alipayConfig.setFormat("JSON");
        //设置字符集
        alipayConfig.setCharset("UTF-8");
        //设置签名类型
        alipayConfig.setSignType("RSA2");
        //实例化客户端
        return new DefaultAlipayClient(alipayConfig);
    }
}
